#include <stdio.h>

#include "__InterruptManager.h"
#include "__ThreadManForUser.h"

Intr intrs[PSP_NUM_INT] = {{0, NULL}};
Context curSubIntrCtx;
s32 curSubIntr = -1;
s32 curIntr = -1;
u32 intrRetAddr = 0;

void __KernelSetSubIntrRetAddr(u32 addr)
{
    intrRetAddr = addr;
}

void __KernelCallIntrLoop()
{
    s32 i;
    for (i = curSubIntr + 1; i < intrs[curIntr].numSubIntrs; i++)
    {
        if (intrs[curIntr].subIntrs[i].enabled == 1)
        {
            __KernelSaveContext(&curSubIntrCtx);

            cpu.r[A0] = i;
            cpu.r[A1] = intrs[curIntr].subIntrs[i].argAddr;
            cpu.r[RA] = intrRetAddr;
            cpu.pc = intrs[curIntr].subIntrs[i].funcAddr;
            curSubIntr = i;
            return;
        }
    }
    curSubIntr = -1;
}

void __KernelCallIntr(s32 intrNum)
{
    curIntr = intrNum;
    if (intrNum < 0 || intrNum >= PSP_NUM_INT)
        return;
    __KernelCallIntrLoop();
}

void __KernelReturnFromSubIntr()
{
    __KernelLoadContext(&curSubIntrCtx);
    if (curSubIntr != -1)
        __KernelCallIntrLoop();
}

